package com.sentiment.jsparser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sentiment.config.Config;
import com.sentiment.webcollector.model.Page;

public class PhantomjsCore {
	// 暂时用绝对路劲代替服务器相对路径，没想到好的解决办法
	// 此处要使用Phantomjs必须更改这个路径为该项目的本地路径
	public String path = Config.projectRootPath;
	public static final Logger LOG = LoggerFactory.getLogger(PhantomjsCore.class);

	// 非同步方法可能导致爬虫线程死锁（错误）
	/**
	 * 解析传入的静态页面，返回动态页面的html
	 * 
	 * @param page
	 *            传入代表静态页面的Page对象
	 * @return
	 */
	public String parser(Page page) {
		Runtime rt = Runtime.getRuntime();
		String execPath = null;
		String jsPath = null;
		if (path == Config.tempSystemUserPath) {
			execPath = path + "/WebContent/Tools/phantomjs.exe";
			jsPath = path + "/WebContent/Tools/phantomjsPort.js";
		} else {
			execPath = path + "/Tools/phantomjs.exe";
			jsPath = path + "/Tools/phantomjsPort.js";
		}
		try {
			StringBuilder sb = new StringBuilder();
			Process p = rt.exec(execPath + " " + jsPath + " " + page.getUrl());
			InputStream is = p.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			Thread.sleep(15000);
			String temp = "";
			// Date date = new Date();
			// long time1 = date.getTime() + 10000;
			// long time2 = date.getTime();
			// temp = br.readLine();
			// while(! temp.contains("</html>")){
			// sb.append(temp);
			// temp = br.readLine();
			// }
			// while(time1 > time2 && (temp=br.readLine())!=null){
			// time2 = new Date().getTime();
			// sb.append(temp);
			// }
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			p.destroy();
			if (sb.length() != 0) {
				// LOG.info(sb.toString());
				return sb.toString();
			}
		} catch (IOException e) {
			LOG.error(e.toString());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			LOG.error(e.toString());
		}
		LOG.error("PhantomjsCore解析未成功！");
		return null;
	}

	/**
	 * @deprecated
	 * @param url
	 * @return
	 */
	public String parser(String url) {
		Runtime rt = Runtime.getRuntime();
		// String path = System.getProperty("user.dir");
		path = "E:/check_sentiment";
		System.out.println(path);
		String execPath = path + "/WebContent/Tools/phantomjs.exe";
		System.out.println(execPath);
		String jsPath = path + "/WebContent/Tools/phantomjsPort.js";
		System.out.println(jsPath);
		try {
			Process p = rt.exec(execPath + " " + jsPath + " " + url);
			InputStream is = p.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			StringBuilder sb = new StringBuilder();
			String temp = "";
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}

			p.destroy();
			return sb.toString().substring(0, 100);
		} catch (IOException e) {
			LOG.error(e.toString());
		}
		return null;
	}

	public static void main(String[] args) {
	}
}
